package com.example.mybatis.bean;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.example.mybatis.utils.validator.DataValidator;
import lombok.Data;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class GraphModel {
    String month; //当前字符串的月份
    Integer total = 0;//当前月的数量


    public static void main(String[] args) {
        //获取当前年的所有月份
        List<GraphModel> graphModelList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int i = 0;
        while (i < 12) {

            calendar.set(Calendar.MONTH, i); //修改月份
            GraphModel graphModel = new GraphModel();
            //获取修改后日期对象，转换成日期字符串
            graphModel.setMonth(DateUtil.format(calendar.getTime(), "yyyy-MM"));
            graphModelList.add(graphModel);
            i++;//月份增加

        }
        System.out.println(graphModelList);
        //模拟数据库数据
        List<GraphModel> graphModelList1 =new ArrayList<>() ;
        GraphModel graphModel = new GraphModel () ;
        graphModel. setMonth("2025-03");
        graphModel. setTotal (100) ;
        graphModelList1. add(graphModel) ;
        graphModel = new GraphModel() ;
        graphModel. setMonth("2025-05") ;
        graphModel. setTotal (1000);
        graphModelList1. add (graphModel);


        //遍历数据，对比月份,返回所有的数量
        List<Integer> totalList = graphModelList.stream().map(item->{
            //循环数据库数据
            graphModelList1.stream()
                    //对比月份是否相同
                    .filter(item1->item.getMonth().equals(item1.getMonth()))
                    .forEach(item1->{
                        //设置当前月份的数量
                        item.setTotal(item1.getTotal());
                    });
            //只返回数量
            return item.getTotal();
        }).collect(Collectors.toList());

        System.out.println(totalList);


    }


}
